import { CommonAjax } from "@/services/modules/common.js";
import router from "../../router";
const commonAjax = new CommonAjax();

let buttonRights = [];
// let tmpNumber = 1 ; // 用来控制函数 getMenuRights 调用次数的


// 要添加的异步基础
let newRouterReduce = {
  path: "/",
  name: "总路由",
  component: () => import("@/layouts/TabsView"),
  children: [],
};

// 字符串转换成 import 函数
function _import(file) {
  if (file == "BlankView") {
    return () => import("@/layouts/" + file + ".vue");
  } else {
    return () => import("@/views/" + file + ".vue");
  }
}

// 解析异步路由
function reduceRoutesFormat(_parent, curData) {
  // console.log("_parent",_parent);
  // console.log("curData",curData);
  curData.forEach((_router) => {
    let template = {
      path: _router.path,
      name: _router.meta.title,
      component: _import(_router.component),
      meta: {
        icon: _router.meta.icon,
      },
      notShow: _router.hidden,
      children: [],
    };
    _parent.push(template);
    if (_router.children) {
      reduceRoutesFormat(template.children, _router.children);
    }
  });
}

export default {
  namespaced: true,

  state: {
    // 记住密码
    // login: {},
    // 用户信息
    info: {},
    // 部门信息
    departments: [],
    // 职务数据
    position: {
      createBy: null,
      createTime: null,
      id: null,
      name: null,
      updateBy: null,
      updateTime: null,
    },

    // 菜单权限数组
    MenuRights: [],
    // 按钮权限数组
    ButtonRights: [],
  },

  mutations: {
    // setLogin(state, value) {
    //   state.login = value;
    // },
    setUserInfo(state, value) {
      state.info = value;
    },
    setDepartments(state, value) {
      state.departments = value;
    },
    setPosition(state, value) {
      state.position = value;
    },
    setMenuRights(state, value) {
      state.MenuRights = value;
    },
    setButtonRights(state, value) {
      state.ButtonRights = value;
    },
  },

  actions: {
    // --- 通过职务ID 获取当前职务名称 ---
    getPositionById({ state, commit }) {
      const post = state.info.post;
      commonAjax.getPositionById(post).then((res) => {
        if (res.success) {
          commit("setPosition", res.result);
        }
      });
    },
    // 获取菜单权限
    getMenuRights({ commit }) {
      let token_url = localStorage.getItem("zhqh-pro-token");
      if(token_url != null){
         // 获取请求
      commonAjax.getUserRightsByToken(token_url).then((res) => {
        if (res.success) {
          let menu = [];
          for (let i = 0; i < res.result.menu[0].children.length; i++) {
            menu.push(res.result.menu[0].children[i]);
          }
          reduceRoutesFormat(newRouterReduce.children, menu); // 处理完后的整个路由菜单
          if(router.options.routes[1].children.length <= 4){
            router.addRoutes([newRouterReduce]);            
          }
          let array = [];
          array.push(newRouterReduce);
          commit("setMenuRights", array);
        }
      });
      }
     
    },
    // 获取所有权限
    getButtonRights({ commit }) {
      let token_url = localStorage.getItem("zhqh-pro-token");
      // 通过token 获取权限数组，获取按钮权限数组
      if(token_url != null){
        commonAjax.getUserRightsByToken(token_url).then((res) => {
          if (res.success) {
            
            buttonRights = []; 
            for (let i = 0; i < res.result.auth.length; i++) {
              buttonRights.push(res.result.auth[i].action);
            }
           
            commit("setButtonRights", buttonRights);
          }
        });
      }
    },
  },

  getters: {
    // 按需返回数据
    getDepsInfo: (state) => {
      
      return state.departments.map((item) => {
        return {
          id: item.id, // 当前部门 id
          departName: item.departName, // 当前部门名称
          parentId: item.parentId, // 上级部门 id
        };
      });
    },
  },
};


